package com.why.model;

import com.why.util.StringUtil;

import java.util.*;

public class MsgModel {
    private String msgName;
    private String msgDesc;
    private List<String> versions = new ArrayList<>();

    private List<OriginParamModel> paramModels = new ArrayList<>();

    private MsgModel childModel;

    public String getMsgName() {
        return msgName;
    }

    public void setMsgName(String msgName) {
        this.msgName = msgName;
    }

    public String getMsgDesc() {
        return msgDesc;
    }

    public void setMsgDesc(String msgDesc) {
        this.msgDesc = msgDesc;
    }

    public List<String> getVersions() {
        return versions;
    }

    public void setVersions(List<String> versions) {
        this.versions = versions;
    }

    public void addVersions(String version) {
        versions.add(version);
    }

    public List<OriginParamModel> getParamModels() {
        return paramModels;
    }

    public void setParamModels(List<OriginParamModel> paramModels) {
        this.paramModels = paramModels;
    }

    public void addParamModels(OriginParamModel paramModel) {
        paramModels.add(paramModel);
    }

    public MsgModel getChildModel() {
        return childModel;
    }

    public void setChildModel(MsgModel childModel) {
        this.childModel = childModel;
    }

    /**
     * 说明：由于要保证在ftl文件中各字段的顺序，以及合并paramName相同的字段信息
     * 本方法采取list存放集合为了保证字段顺序
     * 采用map存在paramName和index则方法合并信息
     */
    public List<OriginParamModel> getCombineParam() {
        List<OriginParamModel> dealModel = new ArrayList<>();
        Map<String, Integer> paramModelMap = new HashMap<>();
        for (OriginParamModel model : paramModels) {
            if (paramModelMap.containsKey(model.getParamName())) {
                OriginParamModel originParamModel = dealModel.get(paramModelMap.get(model.getParamName()));
                originParamModel.setVersion(originParamModel.getVersion() + " & " + model.getVersion());
                if (!originParamModel.getCharLength().equalsIgnoreCase(model.getCharLength())) {
                    originParamModel.setCharLength(originParamModel.getCharLength() + " | " + model.getCharLength());
                }
                continue;
            }
            OriginParamModel paramModel = new OriginParamModel();
            paramModel.setParamName(model.getParamName());
            paramModel.setOutParamName(model.getOutParamName());
            paramModel.setParamDesc(model.getParamDesc());
            paramModel.setOutParamType(model.getOutParamType());
            paramModel.setMsgParamType(model.getMsgParamType());
            paramModel.setParamCharType(model.getParamCharType());
            paramModel.setCharLength(model.getCharLength());
            paramModel.setDefaultValue(model.getDefaultValue());
            paramModel.setVersion(model.getVersion());
            dealModel.add(paramModel);
            paramModelMap.put(model.getParamName(), dealModel.indexOf(paramModel));
        }
        return dealModel;
    }

    public static class OriginParamModel {
        private String paramName;

        private String outParamName;

        private String paramDesc;

        private String outParamType;

        private String msgParamType;

        private String paramCharType;

        private String charLength;

        private String defaultValue;

        private String version;

        public String getParamName() {
            return paramName;
        }

        public void setParamName(String paramName) {
            this.paramName = paramName;
        }

        public String getOutParamName() {
            return outParamName;
        }

        public void setOutParamName(String outParamName) {
            this.outParamName = outParamName;
        }

        public String getParamDesc() {
            return paramDesc;
        }

        public void setParamDesc(String paramDesc) {
            this.paramDesc = paramDesc;
        }

        public String getOutParamType() {
            return outParamType;
        }

        public void setOutParamType(String outParamType) {
            this.outParamType = outParamType;
        }

        public String getParamCharType() {
            return paramCharType;
        }

        public void setParamCharType(String paramCharType) {
            this.paramCharType = paramCharType;
        }

        public String getCharLength() {
            return charLength;
        }

        public void setCharLength(String charLength) {
            this.charLength = charLength;
        }

        public String getDefaultValue() {
            return defaultValue;
        }

        public void setDefaultValue(String defaultValue) {
            this.defaultValue = defaultValue;
        }

        public String getVersion() {
            return version;
        }

        public List<String> getVerList() {
            return Arrays.asList(version.split(","));
        }

        public void setVersion(String version) {
            this.version = version;
        }

        public String getMsgParamType() {
            return msgParamType;
        }

        public void setMsgParamType(String msgParamType) {
            this.msgParamType = msgParamType;
        }

        public String getCharDesc() {
            return paramCharType + (StringUtil.isEmpty(charLength) ? "" : "+" + charLength);
        }

        public String getBaseType() {
            if (outParamType.split(">")[0].equalsIgnoreCase("enum")) {
                return "enum";
            } else if (outParamType.split(">")[0].equalsIgnoreCase("innerclass")) {
                return "innerclass";
            } else {
                return "base";
            }
        }

        public String getInnerClassName() {
            return outParamType.split(">")[1];
        }

        @Override
        public String toString() {
            return "OriginParamModel{" +
                    "paramName='" + paramName + '\'' +
                    ", outParamName='" + outParamName + '\'' +
                    ", paramDesc='" + paramDesc + '\'' +
                    ", paramType='" + outParamType + '\'' +
                    ", msgParamType='" + msgParamType + '\'' +
                    ", paramCharType='" + paramCharType + '\'' +
                    ", charLength='" + charLength + '\'' +
                    ", defaultValue='" + defaultValue + '\'' +
                    ", version='" + version + '\'' +
                    ", base='" + getBaseType() + '\'' +
                    '}';
        }
    }

    @Override
    public String toString() {
        return "MsgModel{" +
                "msgName='" + msgName + '\'' +
                ", msgDesc='" + msgDesc + '\'' +
                ", versions=" + versions +
                ", paramModels=" + paramModels +
                ", childModel=" + childModel +
                '}';
    }
}
